<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use View, DB, Hash, Auth;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getLogin()
    {
        return View::make('user.login');
    }

    public function postLogin(Request $request)
    {
        $this->validate($request, [
            'username' => array('required', 'min:5', 'max:6', 'regex:/^[A-Za-z0-9_]{5,18}$/'),
            'pwd' => array('required', 'min:6', 'max:18', 'regex:/^[A-Za-z0-9!@#%&*]{6,18}$/'),
        ]);
        $user = DB::table('users')->where('username', $request->input('username'))->first();
        if ($user) {
            $bol = Hash::check($request->input('pwd'), $user->password);
        }
        if ($bol) {
            Auth::loginUsingId($user->id);
            return response()->json(array('code' => 200));
        } else {
            return response()->json(array('code' => 10403))->header('status', '403 Forbidd');
        }
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
